﻿spsetBase("mrBranch", "BranchID", "mrBranch as T", "*", "#grid1", "mrbusiness","企业");
var Morgobj;
var Mbankobj;
var Mbusobj;
$(function () {

    //这里data: datebranch  无法加载数据
    var datebranch = "[{\"id\":\"企业\",\"text\":\"企业\"},{\"id\":\"子企\",\"text\":\"子企\"}]";
    Mbusobj = $.parseJSON(datebranch);
    $('#fparentId').combobox({
        editable: false,
        valueField: 'BranchID',
        textField: 'BranchName',
        multiple: false
    });
    $('#ftype').combobox({
        editable: false,
        valueField: 'id',
        textField: 'text',
        multiple: false,

        onChange: function (newValue, oldValue) {
            if (newValue != "企业") {
                $("#fparentId").combobox({ disabled: false });
                //加载企业
                $('#fparentId').combobox({
                    url: sposelUrlByFNoState("BranchID,BranchName", "mrBranch", " type in(hcdhc企业hcdhc)", "", "", "BranchID")
                });
            }
            else {
                $('#fparentId').combobox('disable');
                $('#fparentId').combobox("select", "");

            }
        },
        onLoadSuccess: function () { //加载完成后,设置选中第一项
            var val = $(this).combobox("getData");
            for (var item in val[0]) {
                if (item == "id") {
                    $(this).combobox("select", val[0][item]);
                }
            }
        }
    });
    $('#findtype').combobox({
        editable: false,
        valueField: 'id',
        textField: 'text',
        multiple: false,
        onChange: function (newValue, oldValue) {
        },
        onLoadSuccess: function () {
            var val = $(this).combobox("getData");
            for (var item in val[0]) {
                if (item == "id") {
                    $(this).combobox("select", val[0][item]);
                }
            }
        }
    });
    $("#findtype").combobox("loadData", Mbusobj);
    $("#ftype").combobox("loadData", Mbusobj);
    BindGrid();


});





function BindGrid() {
    $(grid1ID).datagrid({
        pagination: true, //是否显示分页控件
        fitColumns: true, //是否自适应宽度
        fit: true, //是否自适应父容器的大小
        loadMsg: "努力加载中...", //加载数据时，显示的提示信息
        rownumbers: true, //是否显示行号
        singleSelect: false, //是否单选
        remoteSort: false, //是否启用服务器排序
        //sortName: "id", //排序字段
        sortOrder: "ASC", //排序类型
        striped: true, //行显示交替背景
        toolbar: "#tb", //工具栏
        idField: 'fid',
        pageSize: 10,
        toolbar:
        [
        { id: 'btnAdd', text: '新增', iconCls: 'icon-add', handler: toolAdd },
        //{ id: 'btnUpdate', text: '修改', iconCls: 'icon-edit', handler: Update },
{ id: 'btnDel', text: '删除', iconCls: 'icon-delete', handler: toolDelete }
        ],
        // checkOnSelect :false,
        pageList: [10, 20, 30, 40, 50],
        columns: [[
           { field: "BranchID", title: "ID", width: 80, sortable: true, checkbox: true },
           { field: "type", title: "类型", width: 100, sortable: true },
           { field: "BranchName", title: "名称", width: 80, sortable: true },
           { field: "SimpleCode", title: "代码", width: 80, sortable: true },
           { field: "City", title: "位置", width: 100, sortable: true },
           {
               field: "y", title: "操作", width: 50, align: "center", formatter: function (value, row, index) {
                   //return "<a href='#' style='color: #369591;' onclick='javascript:view(" + index + ")'>查看</a>";
                   //return "<a href='#' style='color: #369591;' onclick='javascript:Update(" + row.fid + ")'>修改</a>";
                   return "<a href='#' style='color: #369591;' onclick='javascript:toolUpdateForId(" + index + ")'>修改</a>";
               }
           }
        ]],
        onLoadSuccess: function (data) {
            switch (data.message) {
                case JsonResult.SessionOut:
                    $.messager.alert('系统出错', ' 登录超时，请重新登录!');
                    break;
                case JsonResult.Error:
                    $.messager.alert('系统出错', data.data);
                    break;
            }
        },
        onBeforeLoad: function (param) {
            if (!$(this).datagrid('options').url) {
                $(this).datagrid('options').url = spselUrl();
            }
            $.extend(param, $("#searchForm").formToJson());
        }
    });
}


function Search() {
    $(grid1).datagrid("load");
}

function Reset() {

    $("#findBranchName").val("");
}


//以下可公用
var SetSelectWithObjIdName = function (sel, objData) {
    $(sel).children().remove();
    $(sel).append("<option value='' selected='selected'></option>");
    for (var i = 0; i < objData.length; i++) {
        if (i == 0) {
            $(sel).append("<option value='" + objData[i].id + "' >" + objData[i].name + "</option>");
        } else {
            $(sel).append("<option value='" + objData[i].id + "'>" + objData[i].name + "</option>");
        }

    }

}
